<?php

/**
 * sitemap
 */

//ini_set("display_errors", 1);
//@ini_set("memory_limit",-1);

include_once($_SERVER["DOCUMENT_ROOT"]. "/core/inc.php");

$g_cgival = array();
$g_show = array();
$g_pro = array();


function check_cgi_pro()
{
	global $g_cgival, $g_show, $g_pro;
	
	cgi::both($g_cgival['t'],"t","json");
	$g_cgival['t'] = string::un_html($g_cgival['t']);
	
	cgi::both($g_cgival['callback'],"callback","");
	$g_cgival['callback'] = string::un_html($g_cgival['callback']);
	
	cgi::both($g_cgival['site_id'],"site_id",900);
	$g_cgival['site_id']  = intval($g_cgival['site_id']);
	
	cgi::both($g_cgival['p'],"p",1);
	$g_cgival['p']  = intval($g_cgival['p']);
	
	cgi::both($g_cgival['pcount'],"pcount",1000);
	$g_cgival['pcount']  = intval($g_cgival['pcount']);
	
	cgi::both($g_cgival['type'],"type","rstrt");
	
	if($g_cgival['p'] < 1)
	{
		$g_cgival['p'] = 1;
	}
	
	if($g_cgival['pcount'] > 10000)
	{
		$g_cgival['pcount'] = 10000;
	}
	
}


function get_data()
{
	global $g_cgival, $g_show, $g_pro;
	
	if(!isset($_GET["e_debug"]))
	{
		header("Content-type: text/xml");
	
	}
	
	
	$db_base = new db_base();
	$type = $g_cgival["type"];
	
	$user_global_config = user_global_config::get_instance();
	$site_id = $user_global_config->get_current_site_id();
	$domain = $_SERVER["HTTP_HOST"];
	
	$p = $g_cgival['p'];
	$pcount = $g_cgival['pcount'];
	
	$data = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
	$data .="\n";
	if($type == "rstrt")
	{
		$where_ary[] = "RstrtShow = '1'";
		if($site_id)$where_ary[] = "site_id = {$site_id}";
		
		$where = join(" and ",$where_ary);
		$sql ="select count(*) as count from web_rstrt where {$where}";
		$count = $db_base->select($sql);
		$count = $count[0]["count"];
		
		$page = ceil($count/$pcount);
		
		$start = ($p -1)*$pcount;
		$sql ="select site_id,RstrtID,RstrtDate from web_rstrt where {$where} order by RstrtDate desc limit $start,{$pcount}";
		$rstrt_list = $db_base->select($sql);
		
		$lastmod=date("Y-m-d\TH:i:s+00:00");
		foreach($rstrt_list as $key => $value)
		{
			$url = "http://{$domain}/explore/res-{$value["RstrtID"]}.html";
			$data .= "\t<url><loc>{$url}</loc><lastmod>{$lastmod}</lastmod><changefreq>daily</changefreq><priority>0.5</priority></url>\n";
			
		}
		
	}
	elseif($type == "status")
	{
		$where_ary[] = "status = 1";
		if($site_id)$where_ary[] = "site_id = {$site_id}";
		
		$where = join(" and ",$where_ary);
		$sql ="select count(*) as count from food_status where {$where}";
		$count = $db_base->select($sql);
		$count = $count[0]["count"];
		
		$page = ceil($count/$pcount);
		
		$start = ($p -1)*$pcount;
		$sql ="select site_id,status_id from food_status where {$where} order by last_update_time desc limit $start,{$pcount}";
		$rstrt_list = $db_base->select($sql);
		
		$lastmod=date("Y-m-d\TH:i:s+00:00");
		foreach($rstrt_list as $key => $value)
		{
			if($value["is_rstrt"])
			{
				$url ="http://{$domain}/explore/detail-{$value["status_id"]}.html";
				
			}
			else
			{
				$url ="http://{$domain}/show/detail-{$value["status_id"]}.html";	
			}
			
			$data .= "\t<url><loc>{$url}</loc><lastmod>{$lastmod}</lastmod><changefreq>daily</changefreq><priority>0.5</priority></url>\n";
			
		}
		
	}
	elseif($type== "album")
	{
		$where_ary[] = "status = 1";
		if($site_id)$where_ary[] = "site_id = {$site_id}";
		
		$where = join(" and ",$where_ary);
		$sql ="select count(*) as count from food_album where {$where}";
		$count = $db_base->select($sql);
		$count = $count[0]["count"];
		
		$page = ceil($count/$pcount);
		
		$start = ($p -1)*$pcount;
		$sql ="select site_id,album_id from food_album where {$where} order by last_update_time desc limit $start,{$pcount}";
		$rstrt_list = $db_base->select($sql);
		
		$lastmod=date("Y-m-d\TH:i:s+00:00");
		foreach($rstrt_list as $key => $value)
		{
			
			$url ="http://{$domain}/album/detail-{$value["album_id"]}.html";
			$data .= "\t<url><loc>{$url}</loc><lastmod>{$lastmod}</lastmod><changefreq>daily</changefreq><priority>0.5</priority></url>\n";
			
		}
		
	}
	elseif($type== "status_tag")
	{
		$where_ary[] = "status = 1";
		if($site_id)$where_ary[] = "site_id = {$site_id}";
		
		$where = join(" and ",$where_ary);
		$sql ="select count(*) as count from food_tag where {$where}";
		$count = $db_base->select($sql);
		$count = $count[0]["count"];
		
		$page = ceil($count/$pcount);
		
		$start = ($p -1)*$pcount;
		$sql ="select site_id,tag_hash from food_tag where {$where} order by status_month_count desc  limit $start,{$pcount}";
		$rstrt_list = $db_base->select($sql);
		
		
		$lastmod=date("Y-m-d\TH:i:s+00:00");
		foreach($rstrt_list as $key => $value)
		{
			$url ="http://$domain/explore/tag/{$value["tag_hash"]}";
		
			$data .= "\t<url><loc>{$url}</loc><lastmod>{$lastmod}</lastmod><changefreq>daily</changefreq><priority>0.5</priority></url>\n";
			
			$url="http://$domain/explore/tag/{$value["tag_hash"]}";
			
			$data .= "\t<url><loc>{$url}</loc><lastmod>{$lastmod}</lastmod><changefreq>daily</changefreq><priority>0.5</priority></url>\n";
			
			
		}
		
	}
	elseif($type== "album_tag")
	{
		$where_ary[] = "status = 1";
		if($site_id)$where_ary[] = "site_id = {$site_id}";
		
		$where = join(" and ",$where_ary);
		$sql ="select count(*) as count from food_tag where {$where}";
		$count = $db_base->select($sql);
		$count = $count[0]["count"];
		
		$page = ceil($count/$pcount);
		
		$start = ($p -1)*$pcount;
		$sql ="select site_id,id ,tag_hash from food_album_tag where {$where} limit $start,{$pcount}";
		$rstrt_list = $db_base->select($sql);
		
		$lastmod=date("Y-m-d\TH:i:s+00:00");
		foreach($rstrt_list as $key => $value)
		{
			
			$url ="http://{$domain}/album/tag/{$value["tag_hash"]}/";
			$data .= "\t<url><loc>{$url}</loc><lastmod>{$lastmod}</lastmod><changefreq>daily</changefreq><priority>0.5</priority></url>\n";
			
		}
		
	}
	elseif($type== "user")
	{
		$where_ary[] = "status = 1";
		if($site_id)$where_ary[] = "site_id = {$site_id}";
		
		$where = join(" and ",$where_ary);
		$sql ="select count(*) as count from food_user where {$where}";
		$count = $db_base->select($sql);
		$count = $count[0]["count"];
		
		$page = ceil($count/$pcount);
		
		$start = ($p -1)*$pcount;
		$sql ="select site_id,uid  from food_user where {$where} limit $start,{$pcount}";
		$rstrt_list = $db_base->select($sql);
		
		
		$lastmod=date("Y-m-d\TH:i:s+00:00");
		foreach($rstrt_list as $key => $value)
		{
			
			$url ="http://{$domain}/u/{$value["uid"]}";
			$data .= "\t<url><loc>{$url}</loc><lastmod>{$lastmod}</lastmod><changefreq>daily</changefreq><priority>0.5</priority></url>\n";
			
		}
		
	}
	
	$data .="</urlset>\n";
	echo $data;
}



 
try
{
	check_cgi_pro();
	get_data();

	$g_show["error"] = "0";
	$g_show["errmsg"] = "";
}
catch (Exception $e)
{
	$g_show["error"] = "1";
	$g_show["errmsg"] = $e->getMessage();
}

